const baseModel = require('./base.js');
const yapi = require('../yapi.js');

class dubboInterfaceModel extends baseModel {
  getName() {
    return 'dubbo_interface';
  }

  getSchema() {
    return {
      cname: {type: String, required: true },
      classname: {type: String, required: true },
      desc: String,
      status: {
        type: Number,
        required: false,
        enum: [0, 1, 2, 3]  //0:设计中;1:sit测试;2:uat测试;3:已上线
      },
      // onlinedate: Number,
      onlinedate:String,
      projectid: Number,
      batch: Number,
      ctime: Number,
      cuserid: Number,
      etime: Number,
      euserid: Number,
      version: Number,
      ischl: Boolean,
      chltype: Number
    };
  }

  /**
   * @param {Number} uid 用户id
   * @param {Number} projectid 项目id
   * @param {String} projectname 项目名
   * @param {String} icon 项目图标
   */

  // TODO: data 不完全添加
  save(data) {
    let m = new this.model(data);
    return m.save();
  }

  get(id) {
    return this.model
      .findOne({
        _id: id
      })
      .exec();
  }

  getByPid(id) {
    return this.model
      .findOne({
        _id: id
      })
      .select('projectid')
      .exec()
  }

  //  旧的逻辑错误的代码，可删除
  // checkRepeat(name) {
  //   return this.model.countDocuments({
  //     name: name
  //   });
  // }

  checkRepeat(projectid,classname) {
    return this.model.countDocuments({
      projectid:projectid,
      classname: classname
    });
  }

  countByProjectId(id) {
    return this.model.countDocuments({
      projectid: id
    });
  }

  list(projectid) {
    return this.model
      .find({
        projectid: projectid
      })
      .sort({ index: 1 })
      .exec();
  }

  listWithPage(project_id, page, limit) {
    page = parseInt(page);
    limit = parseInt(limit);
    return this.model
        .find({
          projectid: project_id
        })
        .sort({ cname: 1 })
        .skip((page - 1) * limit)
        .limit(limit)
        .exec();
  }

  listByPid(project_id) {
    return this.model
        .find({
          projectid: project_id
        })
        .sort({ cname: 1 })
        .exec();
  }

  //获取全部接口信息
  getInterfaceListCount() {
    return this.model.countDocuments({});
  }

  del(id) {
    return this.model.remove({
      _id: id
    });
  }

  delByProjectId(id) {
    return this.model.remove({
      projectid: id
    });
  }

  up(id, data) {
    data.up_time = yapi.commons.time();
    return this.model.update(
      {
        _id: id
      },
      data
    );
  }

  listCount(option) {
    return this.model.countDocuments(option);
  }

  upCatIndex(id, index) {
    return this.model.update(
      {
        _id: id
      },
      {
        index: index
      }
    );
  }

  search(keyword) {
    return this.model
        .find({
          cname: new RegExp(keyword, 'ig')
        })
        .limit(10);
  }

  /* TTC ADD */

  // 000
  listByProject(projectid){
    return this.model.find({
      projectid:projectid
    })
  }

  // 001
  listByCNClassRegex(projectid,classRegex){
    return this.model.find({
      projectid:projectid,
      cname:classRegex
    })
  }

  listByENClassRegex(projectid,classRegex){
    return this.model.find({
      projectid:projectid,
      classname:classRegex
    })
  }

  // 010
  listByStatus(projectid,status){
    return this.model
      .find({
        projectid:projectid,
        status:status
      })
  }

  // 011
  listBySnCNC(projectid,status,classRegex){
    return this.model
      .find({
        projectid:projectid,
        status:status,
        cname:classRegex
      })
  }

  listBySnENC(projectid,status,classRegex){
    return this.model
      .find({
        projectid:projectid,
        status:status,
        classname:classRegex
      })
  }

  // 100
  listByOnlinedate(projectid,onlinedate){
    return this.model
      .find({
        projectid:projectid,
        onlinedate:onlinedate
      })
  }

  // 101
  listByOnCNC(projectid,onlinedate,classRegex){
    return this.model
      .find({
        projectid:projectid,
        onlinedate:onlinedate,
        cname:classRegex
      })
  }

  listByOnENC(projectid,onlinedate,classRegex){
    return this.model
      .find({
        projectid:projectid,
        onlinedate:onlinedate,
        classname:classRegex
      })
  }

  // 110
  listByOnS(projectid,onlinedate,status) {
    return this.model
      .find({
        projectid:projectid,
        onlinedate:onlinedate,
        status:status
      })
  }

  // 111
  listByOnSnCNC(projectid,onlinedate,status,classRegex){
    return this.model
      .find({
        projectid:projectid,
        onlinedate:onlinedate,
        status:status,
        cname:classRegex
      })
  }

  listByOnSnENC(projectid,onlinedate,status,classRegex){
    return this.model
      .find({
        projectid:projectid,
        onlinedate:onlinedate,
        status:status,
        classname:classRegex
      })
  }
  /* TTC ADD ^^^^^*/
}

module.exports = dubboInterfaceModel;